package GUI;
import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;


public class MainScreen extends JFrame
{
	private Action New = new NewAction();
	private Action Open = new OpenAction();
    private Action Exit = new ExitAction();
    private Action About = new AboutAction();
    private Action Wiki = new WikiAction();
    private Action Updates = new UpdatesAction();
    private JFileChooser _fileChooser = new JFileChooser();
    private JLabel circuit;
    private BufferedImage originalImage;
    private BufferedImage resizeImageHintPng;
    public MainScreen()
    {
    	super("The Circuit Pro");
    	this.setJMenuBar(CreateMenu());
    	try 
    	{
    		originalImage = ImageIO.read(getClass().getResource("circuit.png"));
			int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
			resizeImageHintPng = resizeImageWithHint(originalImage, type);
		} catch (IOException e) 
		{
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		circuit = new JLabel(new ImageIcon( resizeImageHintPng ));
		add( circuit );

    }
    
    
    
    private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type)
    {
	    	Dimension temp=Toolkit.getDefaultToolkit().getScreenSize();
	    	BufferedImage resizedImage = new BufferedImage(temp.width,temp.height,type);
	    	Graphics2D g = resizedImage.createGraphics();
	    	g.drawImage(originalImage, 0, 0, temp.width,temp.height, null);
	    	g.dispose();	
	    	g.setComposite(AlphaComposite.Src);
	     
	    	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
	    	RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	    	g.setRenderingHint(RenderingHints.KEY_RENDERING,
	    	RenderingHints.VALUE_RENDER_QUALITY);
	    	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	    	RenderingHints.VALUE_ANTIALIAS_ON);
	     
	    	return resizedImage;
    }	
    protected JMenuBar CreateMenu()
	{

		JMenuBar fileBar = new JMenuBar();
				
		JMenu file = new JMenu("File");
	    JMenu help= new JMenu("Help");	    
	   
		file.setMnemonic(KeyEvent.VK_F);		
		help.setMnemonic(KeyEvent.VK_H);
		
		
		fileBar.add(file);
		fileBar.add(help);
		
		file.add(New);
		file.addSeparator();
		file.add(Open);
		file.addSeparator();
		file.add(Exit);
		
		
		help.add(About);
		help.addSeparator();
		help.add(Wiki);
		help.add(Updates);
		return fileBar;
	}
    
    class OpenAction extends AbstractAction 
	{
        
        public OpenAction() 
        {
            super("Open");
            putValue(MNEMONIC_KEY, new Integer('O'));
        }
        
       
        public void actionPerformed(ActionEvent e) 
        {
        	
            int retval = _fileChooser.showOpenDialog(MainScreen.this);
            if (retval == JFileChooser.APPROVE_OPTION) 
            {
                File f = _fileChooser.getSelectedFile();
                try 
                {
                    FileReader reader = new FileReader(f);
               
                } 
                catch (IOException ioex) 
                {
                    System.out.println(e);
                    System.exit(1);
                }
            }
        }
	}
   
    
    
   
    class ExitAction extends AbstractAction
    {
        
   
        public ExitAction() 
        {
            super("Exit");
            putValue(MNEMONIC_KEY, new Integer('X'));
        }
        
   
        public void actionPerformed(ActionEvent e) 
        {
            System.exit(0);
        }
    }
    
    class NewAction extends AbstractAction
    {
        
   
        public NewAction() 
        {
            super("New");
            putValue(MNEMONIC_KEY, new Integer('N'));
        }
        
   
        public void actionPerformed(ActionEvent e) 
        {
        	WorkSpaceScreen obj = new WorkSpaceScreen();
        	obj.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        	obj.setSize(1280,760);
        	obj.setVisible(true);
        }
    }
    
    class AboutAction extends AbstractAction
    {
        
    	private String str;
        public AboutAction() 
        {
            super("About");
            str="Circiut Pro  Project Created by Satyam Krishna & Rajul Bhatnagar";
            putValue(MNEMONIC_KEY, new Integer('A'));
        }
        
   
        public void actionPerformed(ActionEvent e) 
        {
            JOptionPane.showMessageDialog(null, str);
        }
    }
    
    class WikiAction extends AbstractAction
    {
        
   
        public WikiAction() 
        {
            super("Wiki");
            putValue(MNEMONIC_KEY, new Integer('W'));
        }
        
   
        public void actionPerformed(ActionEvent e) 
        {
            
        }
    }
    
    class UpdatesAction extends AbstractAction
    {
        
   
        public UpdatesAction() 
        {
            super("Update");
            putValue(MNEMONIC_KEY, new Integer('U'));
        }
        
   
        public void actionPerformed(ActionEvent e) 
        {
            
        }
    }
}